﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Quart;
	import com.wandian.controller.*;
	import com.wandian.data.GameData;
	import com.wandian.model.*;
	import com.wandian.model.vo.*;
	import com.wandian.util.CLZZoom;

	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;

	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.*;
	/**   
	* @author long
	* @QQ 108232706
	* @version 1.0   
	*/   
	public class AnimationMediator extends Mediator
	{
		private var _arrXY:Array;
		private var _arrT:Array;
		private var _inRoomData:InRoomPlayerDataProxy;
		private var _global:GameData;
		private var _arrArrow:Array;
		private var _arrFace:Array;
		public static const NAME:String="AnimationMediator";

		public function AnimationMediator(param1:Object=null)
		{
			this._arrXY=[];
			this._arrT=[];
			this._global=GameData.getInstance();
			this._arrArrow=[];
			this._arrFace=[];
			super(NAME, param1);
			this._inRoomData=facade.retrieveProxy(InRoomPlayerDataProxy.NAME) as InRoomPlayerDataProxy;
			return;
		} 

		override public function onRegister():void
		{
			this._arrArrow=[this.mcAnimation["arrow1"], this.mcAnimation["arrow2"], this.mcAnimation["arrow3"], this.mcAnimation["arrow4"], this.mcAnimation["arrow5"], this.mcAnimation["arrow6"], this.mcAnimation["arrow7"], this.mcAnimation["arrow8"], this.mcAnimation["arrow9"]];
			var total:*=this._arrArrow.length;
			var index:int=0;
			while (index < total)
			{

				this._arrArrow[index].visible=false;
				this._arrFace[index]=new Sprite();
				this.mcAnimation.addChild(this._arrFace[index]);
				index=index + 1;
			}
			this._arrXY=[this.mcAnimation["xy1"], this.mcAnimation["xy2"], this.mcAnimation["xy3"], this.mcAnimation["xy4"], this.mcAnimation["xy5"], this.mcAnimation["xy6"], this.mcAnimation["xy7"], this.mcAnimation["xy8"], this.mcAnimation["xy9"]];
			this._arrT=[this.mcAnimation["axy1"], this.mcAnimation["axy2"], this.mcAnimation["axy3"], this.mcAnimation["axy4"], this.mcAnimation["axy5"], this.mcAnimation["axy6"], this.mcAnimation["axy7"], this.mcAnimation["axy8"], this.mcAnimation["axy9"]];
			this.showMyFriendMessage(this._global.userInfo.userVo.userid);
			this.mcAnimation.removeChild(this.mcAnimation["message_mc"]);
//			this.mcAnimation["message_mc"].mouseChildren=false;
//			this.mcAnimation["message_mc"].buttonMode=true;
//			this.mcAnimation["message_mc"].addEventListener(MouseEvent.CLICK, this.onMessageHandler);
			return;
		} 

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.FRIEND_TO_REQUEST_RES, CLZNoticeName.FRIEND_RETURN_RES, CLZNoticeName.FACE_RES, CLZNoticeName.SITDOWN_RSP, CLZNoticeName.USE_ANIMATION_PROP, CLZNoticeName.STAND_UP_RSP, CLZNoticeName.OUT_GAME_ROOM_REMOVE];
		} 

		override public function handleNotification(notification:INotification):void
		{
			var _loc_2:Array=null;
			var fromId:uint=0;
			var toId:uint=0;
			var from:PlayerVo=null;
			var to:PlayerVo=null;
			var _loc_7:int=0;
			var data:Object=null;
			var sitDownPlayerVo:PlayerVo=null;
			var mcFace:MovieClip=null;
			var _loc_11:Object=null;
			var _loc_12:Boolean=false;
			var _loc_13:int=0;
			var _loc_14:int=0;
			switch (notification.getName())
			{
				case CLZNoticeName.FRIEND_TO_REQUEST_RES:
				{
					_loc_2=notification.getBody() as Array;
					fromId=_loc_2[0];
					toId=_loc_2[1];
					from=this._inRoomData.getInRoomPlayer(fromId);
					to=this._inRoomData.getInRoomPlayer(toId);
					if (toId == this._global.userInfo.userVo.userid)
					{
						_loc_12=false;
						_loc_13=GameData.arrFriendRequest.length;
						_loc_14=0;
						while (_loc_14 < _loc_13)
						{

							sitDownPlayerVo=GameData.arrFriendRequest[_loc_14] as PlayerVo;
							if (sitDownPlayerVo.userid == fromId)
							{
								_loc_12=true;
								break;
							}
							_loc_14=_loc_14 + 1;
						}
						if (!_loc_12)
						{
							GameData.arrFriendRequest.push(from);
						}
					}
					this.moveRequestFriendICON(from, to);
					break;
				}
				case CLZNoticeName.FRIEND_RETURN_RES:
				{
					_loc_2=notification.getBody() as Array;
					fromId=_loc_2[0];
					toId=_loc_2[1];
					_loc_7=_loc_2[2];
					if (_loc_7 == 0)
					{
						this.showAddFriendSucc(fromId, toId);
					}
					break;
				}
				case CLZNoticeName.FACE_RES:
				{
					data=notification.getBody();
					sitDownPlayerVo=this._inRoomData.getSitPlayerByMid(data.userid);
					if (sitDownPlayerVo == null)
					{
						return;
					}
					TweenLite.killTweensOf(this._arrFace[(sitDownPlayerVo.seatId - 1)]);
					GameData.removeNumChildre(this._arrFace[(sitDownPlayerVo.seatId - 1)]);
					mcFace=GameData.getMovieClip(GameData.getFaceUrl(data.pcate, data.pframe)) as MovieClip;
					this._arrFace[(sitDownPlayerVo.seatId - 1)].addChild(mcFace);
					mcFace.x=(-mcFace.width) / 2;
					mcFace.y=(-mcFace.height) / 2;
					CLZZoom.imageZoomWH(mcFace, 85, 85);
					this._arrFace[(sitDownPlayerVo.seatId - 1)].alpha=1;
					this._arrFace[(sitDownPlayerVo.seatId - 1)].x=this._arrXY[(sitDownPlayerVo.seatId - 1)].x;
					this._arrFace[(sitDownPlayerVo.seatId - 1)].y=this._arrXY[(sitDownPlayerVo.seatId - 1)].y;
					this._arrFace[(sitDownPlayerVo.seatId - 1)].scaleY=0.1;
					this._arrFace[(sitDownPlayerVo.seatId - 1)].scaleX=0.1;
					TweenLite.to(this._arrFace[(sitDownPlayerVo.seatId - 1)], 1, {scaleX: 1, scaleY: 1, onComplete: this.moveFaceFinish, onCompleteParams: [this._arrFace[(sitDownPlayerVo.seatId - 1)]]});
					break;
				}
				case CLZNoticeName.SITDOWN_RSP://播放指引方向
				{
					sitDownPlayerVo=notification.getBody() as PlayerVo;
					if (sitDownPlayerVo.userid == this._global.userInfo.userVo.userid)
					{
						this.showMoveArrow(sitDownPlayerVo.seatId);
					}
					break;
				}
				case CLZNoticeName.USE_ANIMATION_PROP:
				{
					_loc_11=notification.getBody();
					this.useMoveProp(_loc_11.fromPlayer, _loc_11.toPlayer, _loc_11.propVo, _loc_11.objData);
					break;
				}
				case CLZNoticeName.STAND_UP_RSP:
				{
					data=notification.getBody();
					GameData.removeNumChildre(this._arrFace[(data.seatId - 1)]);
					break;
				}
				case CLZNoticeName.OUT_GAME_ROOM_REMOVE:
				{
					this.removeThis();
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		} 

		private function useMoveProp(param1:PlayerVo, param2:PlayerVo, param3:PropVo, param4:Object):void
		{
			var propMc:MovieClip;
			var loadProp:Loader;
			var completeProp:Function;
			var fromPlayer:*=param1;
			var toPlayer:*=param2;
			var propVo:*=param3;
			var objData:*=param4;
			completeProp=function(event:Event):void
			{
				propMc=loadProp.content as MovieClip;
				var _loc_2:*=propMc.getChildAt(0) as MovieClip;
				if (_loc_2 != null)
				{
					_loc_2.gotoAndStop(1);
				}
				propMc.x=_arrXY[(fromPlayer.seatId - 1)].x;
				propMc.y=_arrXY[(fromPlayer.seatId - 1)].y;
				mcAnimation.addChild(propMc);
				if (propVo.pcate == 1)
				{
				}
				if (propVo.pframe == 9)
				{
					TweenMax.to(propMc, 1, {bezier: [{x: _arrT[(toPlayer.seatId - 1)].x, y: _arrT[(toPlayer.seatId - 1)].y - 100}, {x: _arrT[(toPlayer.seatId - 1)].x, y: _arrT[(toPlayer.seatId - 1)].y - 10}], ease: Quart.easeIn, onComplete: inToPlayerXY, onCompleteParams: [propMc]});
				}
				else
				{
					TweenMax.to(propMc, 1, {bezier: [{x: _arrXY[(toPlayer.seatId - 1)].x, y: _arrXY[(toPlayer.seatId - 1)].y - 100}, {x: _arrXY[(toPlayer.seatId - 1)].x, y: _arrXY[(toPlayer.seatId - 1)].y - 10}], ease: Quart.easeIn, onComplete: inToPlayerXY, onCompleteParams: [propMc]});
				}
				loadProp.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeProp);
				return;
			} 
			;
			loadProp=new Loader();
			loadProp.contentLoaderInfo.addEventListener(Event.COMPLETE, completeProp);
			loadProp.load(new URLRequest(propVo.url));
			return;
		} 

		private function inToPlayerXY(param1:MovieClip):void
		{
			var mc:MovieClip;
			var removeMovePropHandler:Function;
			var propMc:*=param1;
			removeMovePropHandler=function():void
			{
				if (mc != null)
				{
					mc.gotoAndStop(1);
				}
				mcAnimation.removeChild(propMc);
				propMc=null;
				return;
			} 
			;
			mc=propMc.getChildAt(0) as MovieClip;
			if (mc != null)
			{
				mc.gotoAndPlay(2);
			}
			TweenLite.to(propMc, 1, {delay: 3, alpha: 0, onComplete: removeMovePropHandler});
			return;
		} 

		/**
		 * 根本坐位id播放对应方向箭头，播放5次
		 * @param seatId
		 */
		private function showMoveArrow(seatId:int):void
		{
			var arrowMc:Sprite=null;
			var count:int=0;
			while (count < 5)
			{

				arrowMc=GameData.getSprite("Arrow");
				arrowMc.x=478;
				arrowMc.y=584;
				arrowMc.alpha=0;
				arrowMc.rotation=this._arrArrow[(seatId - 1)].rotation;
				this.mcAnimation.addChild(arrowMc);
				TweenLite.to(arrowMc, 1, {x: this._arrArrow[(seatId - 1)].x, y: this._arrArrow[(seatId - 1)].y, delay: count * 0.2, alpha: this._arrArrow[(seatId - 1)].alpha - count / 6, onComplete: this.onArrowStop, onCompleteParams: [arrowMc]});
				arrowMc=null;
				count=count + 1;
			}
			return;
		} 

		private function onArrowStop(param1:Sprite):void
		{
			if (param1 != null)
			{
			}
			if (this.mcAnimation.contains(param1))
			{
				this.mcAnimation.removeChild(param1);
				param1=null;
			}
			return;
		} 

		private function moveFaceFinish(param1:Sprite):void
		{
			TweenLite.to(param1, 0.5, {delay: 2, alpha: 0, onComplete: this.removeFaceMc, onCompleteParams: [param1]});
			return;
		} 

		private function removeFaceMc(param1:Sprite):void
		{
			GameData.removeNumChildre(param1);
			return;
		} 

		private function showAddFriendSucc(param1:uint, param2:uint):void
		{
			var _loc_7:FriendVo=null;
			var _loc_3:*=this._inRoomData.getInRoomPlayer(param1);
			var _loc_4:*=this._inRoomData.getInRoomPlayer(param2);
			if (_loc_3 != null)
			{
			}
			if (_loc_4 == null)
			{
				return;
			}
			var _loc_5:*=facade.retrieveProxy(UserInfoProxy.NAME) as UserInfoProxy;
			if (_loc_3.userid == this._global.userInfo.userVo.userid)
			{
				_loc_7=GameData.copyClass(_loc_4, new FriendVo()) as FriendVo;
				_loc_7.state=1;
				_loc_5.addFriend(_loc_7);
				this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.replaceStrValue(GameData.tip["FRIEND_SUCC"], "NAME", _loc_4.name)});
			}
			if (_loc_4.userid == this._global.userInfo.userVo.userid)
			{
				_loc_7=GameData.copyClass(_loc_3, new FriendVo()) as FriendVo;
				_loc_7.state=1;
				_loc_5.addFriend(_loc_7);
				this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.replaceStrValue(GameData.tip["FRIEND_SUCC"], "NAME", _loc_3.name)});
			}
			var _loc_6:*=GameData.replaceStrValue(GameData.tip["FRIEND_SUCC_TO_ALL_PLAYER"], "TO_NAME", _loc_4.name);
			_loc_6=GameData.replaceStrValue(_loc_6, "FROM_NAME", _loc_3.name);
			this.sendNotification(CLZNoticeName.CHAT_RES, {type: 3, msg: _loc_6});
			return;
		} 

		private function removeMcHandler(param1:DisplayObject):void
		{
			if (param1 != null)
			{
			}
			if (this.mcAnimation.contains(param1))
			{
				this.mcAnimation.removeChild(param1);
				param1=null;
			}
			return;
		} 

		private function moveRequestFriendICON(param1:PlayerVo, param2:PlayerVo):void
		{
			if (param1 != null)
			{
			}
			if (param2 == null)
			{
				this.showMyFriendMessage(param2.userid);
				return;
			}
			if (param1.seatId != 0)
			{
			}
			if (param2.seatId == 0)
			{
				this.showMyFriendMessage(param2.userid);
				return;
			}
			var _loc_3:*=GameData.getMovieClip("MessageMc");
			_loc_3.gotoAndStop(1);
			_loc_3.x=this._arrXY[(param1.seatId - 1)].x;
			_loc_3.y=this._arrXY[(param1.seatId - 1)].y;
			this.mcAnimation.addChild(_loc_3);
			TweenLite.to(_loc_3, 1, {x: this._arrXY[(param2.seatId - 1)].x, y: this._arrXY[(param2.seatId - 1)].y, onComplete: this.moveToFriendFinish, onCompleteParams: [_loc_3, param2.userid]});
			_loc_3=null;
			return;
		} 

		private function moveToFriendFinish(param1:MovieClip, param2:uint):void
		{
			if (param1 != null)
			{
			}
			if (this.mcAnimation.contains(param1))
			{
				this.mcAnimation.removeChild(param1);
				param1=null;
			}
			this.showMyFriendMessage(param2);
			return;
		} 

		private function showMyFriendMessage(param1:uint):void
		{
			var _loc_2:int=0;
			if (param1 == this._global.userInfo.userVo.userid)
			{
				_loc_2=GameData.arrFriendRequest.length;
				if (_loc_2 > 0)
				{
					this.mcAnimation["message_mc"].visible=true;
					this.mcAnimation["message_mc"].txt.text="X " + _loc_2;
				}
				else
				{
					this.mcAnimation["message_mc"].visible=false;
					this.mcAnimation["message_mc"].txt.text="";
				}
			}
			return;
		} 

		private function onMessageHandler(event:MouseEvent):void
		{
			var _loc_2:*=GameData.arrFriendRequest.shift() as PlayerVo;
			this.showMyFriendMessage(this._global.userInfo.userVo.userid);
			if (_loc_2 == null)
			{
				return;
			}
			var _loc_3:*=GameData.replaceStrValue(GameData.tip["FRIEND_REQUEST_TO"], "NAME", _loc_2.name);
			var _loc_4:Object={okNoticeName: CLZNoticeName.FRIEND_RETURN_REQ, tip: _loc_3, tipTitle: GameData.tip["SYSTEM_TIP"], isCancel: true, data: _loc_2, okTip: "ADD_FRIEND", cancelTip: "CANCEL_FRIEND", isSendClose: true};
			this.sendNotification(CLZNoticeName.SHOW_WINDOWS_TIP, _loc_4);
			return;
		} 

		private function removeThis():void
		{
			var i:int;
			try
			{
				i;
				while (i < 9)
				{

					GameData.removeNumChildre(this._arrFace[i]);
					i=(i + 1);
				}
			}
			catch (e:Error)
			{
			}
			return;
		} 

		private function get mcAnimation():Sprite
		{
			return viewComponent as Sprite;
		} 

	}
}
